<?php
class Profile_model extends MY_Model
{
	public
		$table = 'profiles',
		$prKey = 'profile_ID',
		$publishedField = 'profile_status'
	;

	function __construct()
	{
		parent::__construct();
			
		$this->fields = array(
			'profile_ID' 		=> '',
			'profile_name' 		=> T_('Профиль'),
			'profile_status' 	=> T_('Статус')
		);
		
		$this->rules = array(
			'profile_ID' 		=> "trim|strip_tags|required|max_length[255]",
			'profile_name' 		=> "trim|strip_tags|required|max_length[255]",
			'profile_status' 	=> "trim|strip_tags|integer|max_length[1]",
		);
		
	}	
	
	function getSelectOptions()
	{
		$this->db->order_by('profile_name');
		$query = $this->db->get($this->table);
		$options = array('0' => T_('-выберите-'));
		
		foreach ($query->result() as $row)
		{
			$options[$row->profile_ID] = $row->profile_name; 
		}
		
		return $options;
	}

	function search()
	{		
		$sql  = 'SELECT *';
		$sql .= ' FROM '. $this->table;
		$sql .= ' ORDER by profile_name';
							
		return $this->db->query($sql)->result();
	}	

	function getProfile($id = null)
	{
		$this->db->where($this->prKey, $id);
		$query = $this->db->get($this->table);
		
		return $query->row();
	}
	

	
	
	function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);
		
		$report = "";
		
		foreach ($items as $id)
		{				  
			  //$data = $this->profile_model->get($id);
			  
			  $res = $this->db->delete($this->table, array($this->prKey => $id));
				
        if (is_null($res))
        {
          $this->log->writeMessage('error', T_('Error. Параметр не удален.'));
          $report .= ",0"; // don't deleted         
        }
        else
        {         
          $this->log->writeMessage('done', T_('Параметр удален.'));
          $report .= ",1"; // deleted
          
          $log['id_admins'] = $this->auth->getID();
          $log['message']   = T_('Параметр <span class="green">'). $data['name'] . T_('</span> удален.');
          $log['location']  = $_SERVER['REQUEST_URI'];
          //$log['data']      = serialize($data);
          $this->watchdog_model->insert($log);          
        } 			
		}
		
		return substr($report, 1);
	}
	
}
?>